package com.neusoft.neuiotms.dcom.service.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityNotFoundException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.constant.ExceptionConstants;
import com.neusoft.bizcore.web.exception.UnitedException;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.converter.ServiceLevelConverter;
import com.neusoft.neuiotms.dcom.dto.ServiceLevelDTO;
import com.neusoft.neuiotms.dcom.model.ServiceLevel;
import com.neusoft.neuiotms.dcom.repository.ServiceLevelRepository;
import com.neusoft.neuiotms.dcom.service.ServiceLevelService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class ServiceLevelServiceImpl implements ServiceLevelService {

    @Autowired
    private ServiceLevelRepository serviceLevelRepository;

    @Autowired
    private ServiceLevelConverter serviceLevelConverter;

    @Override
    public List<ServiceLevelDTO> index(final Searchable searchable) {
        final List<ServiceLevel> model = this.serviceLevelRepository.search(searchable);
        return this.serviceLevelConverter.toListDTO(model);
    }

    @Override
    public Page<ServiceLevelDTO> page(final Searchable searchable, final Pageable pageable) {
        final Page<ServiceLevel> page = this.serviceLevelRepository.search(searchable, pageable);
        return this.serviceLevelConverter.toPageDTO(page);
    }

    @Override
    public ServiceLevelDTO create(final ServiceLevelDTO dto, final UserBean user) {
        if (user != null) {
            dto.setUserCode(user.getUsername());
            dto.setUserName(user.getName());
        }
        final ServiceLevel model = this.serviceLevelConverter.toModel(dto);
        this.serviceLevelRepository.save(model);
        return this.serviceLevelConverter.toDTO(model);
    }

    @Override
    public ServiceLevelDTO update(final Long id, final ServiceLevelDTO dto, final UserBean user) {
        ServiceLevel model;
        try {
            model = this.serviceLevelRepository.getOne(id);
        } catch (final EntityNotFoundException e) {
            ServiceLevelServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
        dto.setUpdateTime(new Date());
        if (user != null) {
            dto.setUpdateUserCode(user.getUsername());
            dto.setUpdateUserName(user.getName());
        }
        model = this.serviceLevelConverter.toModel(dto);
        this.serviceLevelRepository.save(model);
        return this.serviceLevelConverter.toDTO(model);

    }

    @Override
    public ServiceLevelDTO detail(final Long id) {
        ServiceLevel model;
        try {
            model = this.serviceLevelRepository.getOne(id);
            return this.serviceLevelConverter.toDTO(model);
        } catch (final EntityNotFoundException e) {
            ServiceLevelServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
    }

    @Override
    public void delete(final Long id) {
        try {
            this.serviceLevelRepository.deleteById(id);
        } catch (final EntityNotFoundException e) {
            ServiceLevelServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(e.getMessage(), new Object[] {id });
        }
    }

    @Override
    public void updateReadingVolume(final Long id) {
        ServiceLevel model;
        try {
            model = this.serviceLevelRepository.findById(id).get();
            int volume = 0;
            if (model.getReadingVolume() != null) {
                volume = model.getReadingVolume().intValue();
            }
            model.setReadingVolume(++volume);
            this.serviceLevelRepository.save(model);
        } catch (final EntityNotFoundException e) {
            ServiceLevelServiceImpl.log.error(e.getMessage(), e);
            throw new UnitedException(ExceptionConstants.ENTITY_NOT_FOUND, new Object[] {id });
        }
    }

}
